package edu.pitt.dbmi.odie.gapp.gwt.model;

import java.text.DecimalFormat;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

import edu.pitt.dbmi.odie.gapp.gwt.client.dao.ODIE_DaoAnnotation;
import edu.upmc.opi.caBIG.common.CaBIG_UUIdGenerator;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ODIE_Annotation {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key id;
	
	@Persistent
	private String odieId;
	
	@Persistent
	private String odieAnalysisId;
	
	@Persistent
	private String odieDocumentId;
	
	@Persistent
	private Long sPos;
	
	@Persistent
	private Long ePos;
	
	@Persistent
	private String odieClsId ;
	
	@Persistent
	private String ontologyName;

	@Persistent
	private String clsName ;
	
	@Persistent
	private boolean isSelected;
	
	public ODIE_Annotation() {
		this.odieId = CaBIG_UUIdGenerator.getUUID() ;
	}
	
    public String getId() {
        return this.odieId ;
    }
    
    public String getOdieId() {
        return this.odieId ;
    }
    
    public void setOdieId(String odieId) {
    	this.odieId = odieId ;
    }
    
	public String getOdieAnalysisId() {
		return odieAnalysisId;
	}

	public void setOdieAnalysisId(String odieAnalysisId) {
		this.odieAnalysisId = odieAnalysisId;
	}
	
	public String getOdieDocumentId() {
		return odieDocumentId;
	}

	public void setOdieDocumentId(String odieDocumentId) {
		this.odieDocumentId = odieDocumentId;
	}

	public Long getSPos() {
		return sPos;
	}

	public void setSPos(Long pos) {
		sPos = pos;
	}

	public Long getEPos() {
		return ePos;
	}

	public void setEPos(Long pos) {
		ePos = pos;
	}

	public String getOdieClsId() {
		return odieClsId;
	}

	public void setOdieClsId(String odieClsId) {
		this.odieClsId = odieClsId;
	}
	
	
	public String getOntologyName() {
		return ontologyName;
	}

	public void setOntologyName(String ontologyName) {
		this.ontologyName = ontologyName;
	}

	public String getClsName() {
		return clsName;
	}

	public void setClsName(String clsName) {
		this.clsName = clsName;
	}
	
	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}
	
    public static String formatLongAsDigitString(Long value) {
    	String digitPattern = "0000" ;
        String retValue = null;
        try {
            DecimalFormat decimalFormat = new DecimalFormat(digitPattern);
            retValue = decimalFormat.format(value) ;
        } catch (Exception x) {
            retValue = null;
        }
        return retValue;
    }
	
	public ODIE_DaoAnnotation convertToClientSideRecord() {
		ODIE_DaoAnnotation result = new ODIE_DaoAnnotation() ;
		result.setOdieId(this.odieId) ;
		result.setOdieAnalysisId(this.odieAnalysisId) ;
		result.setOdieDocumentId(this.odieDocumentId) ;
		result.setSPos(formatLongAsDigitString(getSPos())) ;
		result.setEPos(formatLongAsDigitString(getEPos())) ;
		result.setOntologyName(this.ontologyName) ;
		result.setClsName(this.clsName) ;
		result.setSelected(this.isSelected) ;
		return result ;
	}
    
	public String toXml() {
		StringBuffer sb = new StringBuffer() ;
		if (id != null) {
			sb.append("<annotation>") ;
			sb.append("<id>"+getOdieId()+"</id>") ;
			sb.append("<odieAnalysisId>"+getOdieAnalysisId()+"</odieAnalysisId>") ;
			sb.append("<odieDocumentId>"+getOdieDocumentId()+"</odieDocumentId>") ;
			sb.append("<sPos>"+ formatLongAsDigitString(getSPos())+"</sPos>") ;
			sb.append("<ePos>"+formatLongAsDigitString(getEPos())+"</ePos>") ;
			sb.append("<ontologyName>"+getOntologyName()+"</ontologyName>") ;
			sb.append("<clsName>"+getClsName()+"</clsName>") ;
			sb.append("<isSelected>" + isSelected() + "</isSelected>") ;
			sb.append("</annotation>") ;
		}
		return sb.toString();
	}
}
